package uk.co.hephistocles.blobGame;

import java.util.Vector;

import com.badlogic.gdx.math.Vector2;

public class GField {
	public Vector<Line> outline;
	public float dir;
	int minX, minY, maxX, maxY;
	
	public GField(BezierPath path, float dir) {
		outline = path.toLines();
		this.dir = dir;

		Line line = outline.get(0);
		minX =line.x1;
		minY = line.y1;
		maxX = line.x1;
		maxY = line.y1;
		for (int i=1; i<outline.size(); i++) {
			line = outline.get(i);
			if (minX > line.x1)
				minX = line.x1;
			if (maxX < line.x1)
				maxX = line.x1;
			if (minY > line.y1);
				minY = line.y1;
			if (maxY < line.y1)
				maxY = line.y1;
		}
	}
	
	public boolean contains(float x, float y) {
		//if the point is outside the basic bounding box, it no in polygon
		if ((x<minX) || (x>maxX)){
			if ((y<minY) || (y>maxY)){
				return false;
			}
		}
		System.out.println("Passed the boundingBox test");
		
		//Get axes of the outline
		Vector<Vector2> axesVector = new Vector<Vector2>();
		int i, j;
		for (i=0; i<outline.size(); i++){
			Line l = outline.get(i);
			Vector2 v = new Vector2(l.y1 - l.y2, l.x2 - l.x1);
			Line p = outline.get(0);
			float point = v.dot(new Vector2(p.x1, p.y1));
			float min = point, max = point;
			for (j=1; j<outline.size(); j++) {
				p = outline.get(j);
				point = v.dot(new Vector2(p.x1, p.y1));
				if (min>point)
					min = point;
				if (max<point)
					max = point;
			}
			float c = v.dot(new Vector2(x,y));
			if ((c<min) || (c>max))
				return false;
		}
		return true;
		
		/*
		//start ray outside the polygon to the point
		Line ray = new Line(x,y, minX-2,minY-2);
		Line line;
		System.out.println("Line going from " + x + ", " + y + " to " + (minX-2) + ", " + (minY-2));
		int crossings = 0;
		for (int i=1; i<outline.size(); i++) {
			line = outline.get(i);
			if (ray.intersects(line))
				crossings++;
		}
		System.out.println("Intersects " + crossings + " times ");
		//if it crosses an even number of times, it is outside polygon
		return (crossings%2==0);*/
	}
}